//
//  MLWaterWaveView.m
//  Taegyo
//
//  Created by user on 16/8/15.
//  Copyright © 2016年 wisedu. All rights reserved.
//

#import "MLWaterWaveView.h"

@implementation MLWaterWaveView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self=[super initWithFrame:frame]) {
        
        self.borderWidth = 4;
        self.strokeColor = COLOR(78, 178, 169);
        self.fillColor = [UIColor clearColor];
        self.fromValue = 0.0;
    }
    return self;
}

- (void)setBorderWidth:(CGFloat)borderWidth{
    
    _borderWidth = borderWidth;
    
    [self setNeedsDisplay];
}
- (void)setStrokeColor:(UIColor *)strokeColor{
    
    _strokeColor = strokeColor;
    
    [self setNeedsDisplay];
    
}

- (void)setFillColor:(UIColor *)fillColor{
    
    _fillColor = fillColor;
    
    [self setNeedsDisplay];
}

-(void)setFromValue:(CGFloat)fromValue{
    
    _fromValue = fromValue;
    
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
  
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
    [super drawRect:rect];
    [[UIColor whiteColor] setFill];
    UIRectFill(rect);
    NSInteger pulsingCount = 2;
    double animationDuration = 3;
    CALayer * animationLayer = [CALayer layer];
    for (int i = 0; i < pulsingCount; i++) {
        CALayer * pulsingLayer = [CALayer layer];
        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        pulsingLayer.borderColor = COLOR(22, 163, 130).CGColor;
        pulsingLayer.borderWidth = self.borderWidth;
        pulsingLayer.backgroundColor = self.fillColor.CGColor;
        pulsingLayer.cornerRadius = rect.size.height / 2;
        
     //  CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
        
        CAAnimationGroup * animationGroup = [CAAnimationGroup animation];
        animationGroup.fillMode = kCAFillModeBackwards;
        animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration / (double)pulsingCount;
        animationGroup.duration = animationDuration;
        animationGroup.repeatCount = HUGE;
     //   animationGroup.timingFunction = defaultCurve;
        
        CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.fromValue = @(self.fromValue);
        scaleAnimation.toValue = @1.0;
        
        CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
     
        animationGroup.animations = @[scaleAnimation, opacityAnimation];
        [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
        
        [animationLayer addSublayer:pulsingLayer];
    }
    [self.layer addSublayer:animationLayer];
    
}


@end
